package com.F05.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F05.actionform.F0502ActionForm;
import com.F05.dao.F0502DAO;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;
import com.util.SessionUtils;

/**
 * 
 * @file F0502Action.java
 * @author huqy
 * @since 2009-3-15
 * @version 1.0
 */
public class F0502Action extends BaseAction {

	/**
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	Logger log = Logger.getLogger(F0502Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		F0502ActionForm actionForm = (F0502ActionForm) form;
		F0502DAO dao = new F0502DAO();
		String operationType = actionForm.getOperationType();
		String strDetail="detail";
		String strList = "list";
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		try {
			//操作记录
			t_YhczxxInfo = setYhczxx(request, response, operationType,"F0502");
		
			if(Constants.ADD.equals(operationType)){
				doAdd(actionForm);
				return mapping.findForward(strDetail);	
			}
			
			if(Constants.SAVE_INSERT.equals(operationType)){
				if(doInsert(actionForm,dao)){	
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
		    		return forward;
				}else{
					return mapping.findForward("detail");					
				}
			}
		
			if(Constants.SAVE_UPDATE.equals(operationType)){
				if(doUpdate(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
		    		return forward;
				}else{
					return mapping.findForward("detail");		
				}
			}

			if(Constants.EDIT.equals(operationType)){
				doEdit(actionForm,dao);
                return mapping.findForward("detail");
			}
	
			if(Constants.DELETE.equals(operationType)){
				if(doDelete(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList");
		    		return forward;
				}else{
					return mapping.findForward("list");		
				}
			}
		
			
//			if(Constants.BACK.equals(operationType)){
//				return mapping.findForward(strList);
//			}

			
			if(Constants.QUERYLIST.equals(operationType)){
				actionForm.setList(dao.queryList(actionForm));
				return mapping.findForward(strList);
			}
	
			if(Constants.INIT.equals(operationType)){
				return mapping.findForward(strList);
			}
			
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}
		return mapping.findForward(strList);
	}
	
	private void  doEdit(F0502ActionForm actionForm,F0502DAO dao) throws SelectException{
		try {
			
		    Map map = null;
		    List list = new ArrayList();
			list = dao.selectById(actionForm.getId());
			 for(int i = 0; i < list.size();i++){
				map = (Map)list.get(i);	

				actionForm.setId(CommonTools.null2String((String)map.get("ID")));
				actionForm.setYear(CommonTools.null2String((String)map.get("year")));
				actionForm.setCost(CommonTools.null2String((String)map.get("cost")));
				actionForm.setLayOutType("");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	private boolean doDelete(F0502ActionForm actionForm,F0502DAO dao){
		boolean flag=false;
		try {
			String id=CommonTools.null2String(actionForm.getId());
			flag=dao.delete(id, actionForm);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	private void doAdd(F0502ActionForm actionForm){
		try {
			
			actionForm.setLayOutType("add");
			clearForm(actionForm);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void clearForm(F0502ActionForm actionForm){
		actionForm.setId("");
		actionForm.setYear("");
		actionForm.setYear1("");
		actionForm.setYear2("");
		actionForm.setCost("");
		
	}
	
	private boolean doInsert(F0502ActionForm actionForm,F0502DAO dao){
		boolean flag=false;
		try {
			flag=dao.insert(actionForm);	
			clearForm(actionForm);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return flag;
	}
	
	private boolean doUpdate(F0502ActionForm form,F0502DAO dao){
		boolean flag=false;
		try {
			form.setErrorFlag("");
			flag=dao.update(form);
			clearForm(form);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;	
	}
	
}
